package com.lzx.java.smack.concurrent;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import org.junit.Test;

public class BlockingQueueTest {

	@Test
	public void test() {
		// fail("Not yet implemented");
	}

	@Test
	public void testArrayBlockingQueue() {
		final int size = 3;
		ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(
				size);
		blockingQueue.add("1");
		assertEquals(size - 1, blockingQueue.remainingCapacity());

		List<String> list = new ArrayList<String>();
		blockingQueue.drainTo(list);

		assertEquals(size, blockingQueue.remainingCapacity());

		System.out.println(blockingQueue.remainingCapacity());

		SecurityManager s = System.getSecurityManager();
		System.out.println(s);

		ExecutorService executorService = Executors.newCachedThreadPool();

		for (int i = 0; i < 2000; i++) {
			System.out.println(i);
			executorService.execute(new Runnable() {

				@Override
				public void run() {
					Random randon = new Random();
					try {
						Thread.sleep((long)randon.nextInt(10000));
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			});
		}

		try {
			Thread.sleep(1000L);
			ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)executorService;
			System.out.println(threadPoolExecutor.getQueue().size());
			System.out.println(threadPoolExecutor.getQueue().remainingCapacity());
			
			Thread.sleep(100000L);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
